package com.peas.platform.module.icims.base.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.auth.api.resp.UserVisitResp;
import com.peas.platform.module.icims.base.auth.controller.vo.UserVisitVo;
import com.peas.platform.module.icims.base.auth.domain.UserVisitDomain;
import com.peas.platform.module.icims.base.auth.dto.UserVisitDto;
import com.peas.platform.module.icims.base.auth.service.UserVisitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserVisitServiceImpl extends BaseServiceImpl<UserVisitDto> implements UserVisitService {

    @Resource
    private UserVisitDomain userVisitDomain;

    @Override
    public BaseDomain<UserVisitDto, ? extends BasePo> getDomain() {
        return userVisitDomain;
    }

    @Override
    public List<UserVisitResp> getUserVisit(Long userId) {
        return userVisitDomain.getUserVisit(userId);
    }

    @Transactional
    @Override
    public void addMenuClick(UserVisitVo vo) {
        Assert.notBlank(vo.getName(), "名称不能为空");
        UserVisitDto c = new UserVisitDto();
        c.setUserId(vo.getUserId());
        c.setName(vo.getName());
        UserVisitDto dto = this.getOne(c, false);
        if (dto != null) {
            dto.setClick(dto.getClick() + 1);
            dto.setTitle(vo.getTitle());
            dto.setIcon(vo.getIcon());
            dto.setPath(vo.getPath());
            dto.setLastVisitTime(DateTime.now());
            this.updateById(dto);
        } else {
            dto = BeanUtil.copyProperties(vo, UserVisitDto.class);
            dto.setClick(1);
            dto.setLastVisitTime(DateTime.now());
            this.save(dto);
        }
    }
}
